package dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class PlaceDao {
	private Log log = LogFactory.getLog(PlaceDao.class);
	SessionFactory sf= new Configuration().configure().buildSessionFactory();
	Session session = sf.openSession();
	//更新信息
	public void update(String hql) {
		try {
			Transaction trans = session.beginTransaction();
			Query queryUpdate = session.createQuery(hql);
			int ret = queryUpdate.executeUpdate();	//要修改成使用update
			trans.commit();
			//System.out.println("更新数据-返回值："+ret);
		}catch(Exception e) {
			log.error("update by hql failed", e);
		}finally {
			session.close();
		}
	}
	
	//得到用户账户下订单信息
	public List getOrders(String hql) {
		log.debug("finding orders by hql");
		//读取 Hibernate 配置文件 生成 SessionFactory，对Session进行管理
		SessionFactory sf= new Configuration().configure().buildSessionFactory();
		
		//打开一个session
		Session session=sf.openSession();
		try {
			String queryString = hql;
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by hql failed", re);
			throw re;
		} finally{
			//对应打开 这里关闭
			session.close();
		}
	}
}
